//
//  EditShopContentCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/14.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "EditShopContentCell.h"


@interface EditShopContentCell()<UITextFieldDelegate>


@end


@implementation EditShopContentCell

- (void)setupUI
{
    [self addSubview:self.titleLab];
    [self addSubview:self.textfeild];
}

- (void)setupLayout
{
    _titleLab.size = CGSizeMake(100, 20);
    _titleLab.mj_x = 15;
    _titleLab.mj_y = 18;
    
    _textfeild.size = CGSizeMake(KScreenWidth - 30, 20);
    _textfeild.mj_x = 15;
    _textfeild.mj_y = _titleLab.bottom +10;
    
}

//iOS TextField输入框点击键盘时随着键盘上移
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (self.keyboardShowBlock) {
        self.keyboardShowBlock();
    }
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.keyboardHideBlock) {
        self.keyboardHideBlock();
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger strLength = textField.text.length - range.length + string.length;
    if (strLength > 20){
        
        if (self.wordPromptBlock) {
            self.wordPromptBlock();
        }

        return NO;
    }
    NSString *text = nil;
    //如果string为空，表示删除
    if (string.length > 0) {
        text = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }else{
        text = [textField.text substringToIndex:range.location];
    }
    
    if (text.length > 20) {
        
        return NO;
    }
    
    return YES;
}

#pragma mark - setter methods
#pragma mark - getter methods

- (UILabel *)titleLab{
    
    if (!_titleLab) {
        _titleLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x262626) font:mFont(16) textAliment:0];
    }
    return _titleLab;
}


- (UITextField *)textfeild{
    
    if (!_textfeild) {
        _textfeild = [[UITextField alloc]init];
        _textfeild.textColor = UIColorFromRGB(0x363636);
        _textfeild.font = mFont(14);
        _textfeild.keyboardType = UIKeyboardTypeDefault;
        _textfeild.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textfeild.returnKeyType = UIReturnKeyDone;
        _textfeild.delegate = self;
    }
    return _textfeild;
}

@end
